//
//  MTShopViewController.m
//  TestPrj
//
//  Created by 朱铅峰 on 2020/6/20.
//  Copyright © 2020 Xing. All rights reserved.
//

#import "MTShopViewController.h"
#import <UIViewController+HBD.h>
#import "MTShopSingleModel.h"
#import "ShopNavBarView.h"
#import "ShopHeaderView.h"
#import "ShopActivityView.h"
#import "ShopTabScrollView.h"


@interface MTShopViewController ()<ShopTabScrollViewDelegate,UIGestureRecognizerDelegate>

proStrong ShopNavBarView *navBarView;
proStrong ShopHeaderView *headerView;
proStrong ShopActivityView *activityView;
proStrong ShopTabScrollView *shopTabView;

@end

@implementation MTShopViewController

- (void)setUI {
    
    WEAKSELF
    
    self.headerView = [[ShopHeaderView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, [MTShopSingleModel sharedInstance].headerHeight)];
    [self.view addSubview:self.headerView];
    
    self.activityView = [[ShopActivityView alloc] initWithFrame:CGRectMake(0, [MTShopSingleModel sharedInstance].headerHeight, self.view.width, self.view.height-[MTShopSingleModel sharedInstance].headerHeight)];
    [self.view addSubview:self.activityView];
    
    self.navBarView = [[ShopNavBarView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, kNavBarHeight)];
    self.navBarView.navBarViewBackAction = ^{
        [weakSelf navBackAction];
    };
    [self.view addSubview:self.navBarView];
    
    [self createMoveUpView];
    
    self.shopTabView = [[ShopTabScrollView alloc] initWithFrame:CGRectMake(0, kNavBarHeight, self.view.width, self.view.height-kNavBarHeight)];
    self.shopTabView.showsVerticalScrollIndicator = NO;
    self.shopTabView.scrollDelegate = self;
    [self.view addSubview:self.shopTabView];
}

/// 恢复初始控制视图
- (void)createMoveUpView {
    
    //渐变
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.colors = @[(__bridge id)[UIColor clearColor].CGColor, (__bridge id)[[UIColor blackColor] colorWithAlphaComponent:0.5].CGColor];
    gradientLayer.locations = @[@0.0, @1.0];
    gradientLayer.startPoint = CGPointMake(0, 0);
    gradientLayer.endPoint = CGPointMake(0, 1.5);
    gradientLayer.frame = CGRectMake(0, self.view.height-30, self.view.width, 30);
    [self.view.layer addSublayer:gradientLayer];
    
    UIImageView *imageView = [[UIImageView alloc]init];
    imageView.frame = CGRectMake((self.view.width - 20)/2, self.view.height-27, 20, 20);
    imageView.userInteractionEnabled = YES;
    imageView.image = [UIImage imageNamed:@"icon-Slidingback"];
    [self.view addSubview:imageView];
    
    UIView *view = [[UIView alloc]init];
    view.frame = CGRectMake(0, self.view.height-90, self.view.width, 90);
    [self.view addSubview:view];
    
    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeClick:)];
    swipe.delegate = self;
    swipe.direction = UISwipeGestureRecognizerDirectionUp;
    [view addGestureRecognizer:swipe];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(moveUpTap:)];
    tap.delegate = self;
    [view addGestureRecognizer:tap];
}


- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    self.hbd_barHidden = YES;
    
    [self setUI];
}

/// 导航栏返回按钮事件
- (void)navBackAction {
    
    if (self.shopTabView.contentOffset.y >= [MTShopSingleModel sharedInstance].maxOffsetY) {
        //[self moveUpClick:nil];
    } else {
        [self.navigationController popViewControllerAnimated:YES];
    }
}

/// 回复原样
/// @param btn <#btn description#>
- (void)moveUpClick:(UIButton *)btn {
    
    [UIView animateWithDuration:0.3 animations:^{
        [self.headerView recovery];
        self.shopTabView.isStopAnimation = NO;
        self.shopTabView.transform = CGAffineTransformMakeTranslation(0, 0);//恢复原位置
    } completion:^(BOOL finished) {
    }];
}

- (void)swipeClick:(UISwipeGestureRecognizer *)swipe {
    [self moveUpClick:nil];
}

- (void)moveUpTap:(UITapGestureRecognizer *)tap {
    [self moveUpClick:nil];
}

#pragma mark - ShopTabScrollViewDelegate

//监听列表滚动视图的偏移量
- (void)ShopTabScrollViewDidScroll:(UIScrollView *)scrollView {
    
    CGFloat offset = scrollView.contentOffset.y;
    
    if (scrollView == self.shopTabView) {
        
        [self.navBarView refreshWithOffsetY:offset];
        [self.headerView refreshWithOffsetY:offset];
    }
}

//偏移结束后
- (void)ShopTabScrollViewEndDragging:(UIScrollView *)scrollView {
    
    CGFloat offset = scrollView.contentOffset.y;
    if (offset <= -80) {
        //如果下滑偏移量小于-80,禁止动画执行
        self.shopTabView.isStopAnimation = YES;
    } else {
        self.shopTabView.isStopAnimation = NO;
    }
}

//点击顶部视图  让该视图平移向下消失
- (void)ShopTabScrollViewDropDown:(UIScrollView *)scrollView {
    
#pragma mark 处理消失动作
    [UIView animateWithDuration:0.3 animations:^{
        scrollView.transform = CGAffineTransformMakeTranslation(0, self.view.height);
        [self.headerView changeMax];
        self.shopTabView.isStopAnimation = NO;
    } completion:^(BOOL finished) {
        self.shopTabView.isStopAnimation = YES;
        self.shopTabView.contentOffset = CGPointZero;
    }];
    
    //导航条恢复原状
    [self.navBarView recovery];
}

#pragma mark - 手势
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    return YES; //多个手势共存
}

@end
